package com.snow.useviewdemo.function.copy;

import android.os.Bundle;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.snow.common.base.BaseBasesActivity;
import com.snow.common.tool.utils.ToastUtils;
import com.snow.useviewdemo.R;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * author : zyt
 * e-mail : 632105276@qq.com
 * date   : 2019-08-26
 * desc   :
 */
public class CopyTestAct extends BaseBasesActivity {
    @BindView(R.id.tv_01)
    TextView tv01;
    @BindView(R.id.tv_02)
    TextView tv02;
    @BindView(R.id.tv_03)
    TextView tv03;
    @BindView(R.id.tv_04)
    TextView tv04;
    @BindView(R.id.tv_05)
    TextView tv05;




    @Override
    protected int getContentViewId() {
        return R.layout.actvity_copy_test;
    }

    @Override
    protected void initView() {
        String testMsg = "最近在项目中用到了 PopUpWindow，并且在机型适配时发现华为等具有虚拟按键的手机在横屏状态时会造成 PopUpWindow 显示位置偏移的情况存在，最后完美解决了这个问题，所以把经验分享出来，看能否对你有用。";
        tv01.setText(testMsg);
        tv02.setText("A======" + testMsg);
        tv03.setText("自定义=====" + testMsg);
        tv04.setText(testMsg);
        tv05.setText("A======" + testMsg);
        SelectableTextManager.init().addTextCopy(tv02);
        SelectableTextManager.init().addTextCopy(tv04);
        SelectableTextManager.init().addTextCopy(tv05);
        SelectableTextManager.init().setSelectOptionListener(new SelectableTextManager.SelectOptionListener() {
            @Override
            public void optionAction(String optionResult) {
                ToastUtils.getInstance().showToast(optionResult);
            }
        });
        tv03.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtils.getInstance().showToast("sdfgg");
                SelectableTextManager.init().hindAll();
            }
        });
        findViewById(R.id.rl_root).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                SelectableTextManager.init().hindAll();
                return false;
            }
        });
//        SelectableTextHelper.init().builder(new SelectableTextHelper.Builder(tv02)
//                .setSelectedColor(getResources().getColor(R.color.tt_blue))
//                .setCursorHandleSizeInDp(20)
//                .setCursorHandleColor(getResources().getColor(R.color.tt_blue)));
//
//        SelectableTextHelper.init().setSelectListener(new OnSelectListener() {
//            @Override
//            public void onTextSelected(CharSequence content) {
//                ToastUtils.getInstance().showToast(String.valueOf(content));
//            }
//        });

//        SelectableTextHelper.init().builder(new SelectableTextHelper.Builder(tv05)
//                .setSelectedColor(getResources().getColor(R.color.tt_blue))
//                .setCursorHandleSizeInDp(20)
//                .setCursorHandleColor(getResources().getColor(R.color.tt_blue)));
//        tv02.setOnLongClickListener(new View.OnLongClickListener() {
//            @Override
//            public boolean onLongClick(View v) {
//                SelectableTextHelper.init().builder(new SelectableTextHelper.Builder(tv02)
//                        .setSelectedColor(getResources().getColor(R.color.tt_blue))
//                        .setCursorHandleSizeInDp(20)
//                        .setCursorHandleColor(getResources().getColor(R.color.tt_blue)));
//                return false;
//            }
//        });
//        tv05.setOnLongClickListener(new View.OnLongClickListener() {
//            @Override
//            public boolean onLongClick(View v) {
//                SelectableTextHelper.init().builder(new SelectableTextHelper.Builder(tv05)
//                        .setSelectedColor(getResources().getColor(R.color.tt_blue))
//                        .setCursorHandleSizeInDp(20)
//                        .setCursorHandleColor(getResources().getColor(R.color.tt_blue)));
//                return false;
//            }
//        });
        tv03.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
            private Menu mMenu;

            @Override
            public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
                menu.clear();
                MenuInflater menuInflater = actionMode.getMenuInflater();
                menuInflater.inflate(R.menu.select_menu2, menu);
                return true;
            }

            @Override
            public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
                //菜单创建完成以后获取到其对象，便于后续操作
                this.mMenu = menu;
                return true;
            }

            @Override
            public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
                switch (menuItem.getItemId()) {
                    case R.id.it_all:
                        //全选
//                        tv03.selectAll();
                        ToastUtils.getInstance().showToast("完成全选");
                        break;
                    case R.id.it_copy:
//                        String selectText = getSelectText(SelectMode.COPY);
//                        //setText(selectText)是为了后面的this.mMenu.close()起作用
//                        mTvSelect.setText(selectText);
                        ToastUtils.getInstance().showToast("选中的内容已复制到剪切板");
                        this.mMenu.close();
                        break;
                    case R.id.it_cut:
                        //剪切
//                        String txt = getSelectText(SelectMode.CUT);
//                        mTvSelect.setText(txt);
                        ToastUtils.getInstance().showToast("选中的内容已剪切到剪切板");
                        this.mMenu.close();
                        break;

                    case R.id.it_paste:
                        ToastUtils.getInstance().showToast("粘贴");
//                        //获取剪切班管理者
//                        ClipboardManager cbs = (ClipboardManager) mContext.getSystemService(CLIPBOARD_SERVICE);
//                        if (cbs.hasPrimaryClip()){
//                            mTvSelect.setText(cbs.getPrimaryClip().getItemAt(0).getText());
//                        }
                        this.mMenu.close();
                        break;
                }
                return true;
            }

            @Override
            public void onDestroyActionMode(ActionMode actionMode) {

            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
    }


}